import jieba
from python.apis.chatbot import ChatBot
from service.map import Map
from service.weather import WeatherInfo
from service.fetch_stock_data import fetch_stock_data, find_stock_code
from service.fetch_stock_name import extract_stock_names


# 中文分词
def chinese_segmentation(text):
    return jieba.lcut(text)


# 定义关键信息和对应的操作
key_info_actions = {
    "天气": "调用天气API",
    "经纬度": "调用经纬度API",
    "股票": "调用股票API"
}

# 用户输入问题
user_input = input("请输入您的问题：")
wxAPI = True
# 中文分词
tokens = chinese_segmentation(user_input)
print(tokens)
# 提取关键信息
key_info = []
city = ""

for token in tokens:
    for key in key_info_actions:
        if key in token:
            action = key_info_actions[key]
            key_info.append(key)
            break
    else:
        if any(substring in token for substring in ["省", "市", "区", "县"]):
            city += token

# print(city)
# 如果存在关键信息，则执行相应操作
if key_info:
    wxAPI = False
    for key in key_info:
        action = key_info_actions[key]
        if action == "调用天气API":
            # 在这里执行调用天气API的操作
            data = Map.tengxun(city)
            print("城市：", city)
            weather_info = WeatherInfo()
            weather_info.get_weather_info(data)
        elif action == "调用经纬度API":
            # 在这里执行获取最新新闻的操作
            print("调用经纬度API")
        elif action == "调用股票API":
            # 在这里执行查询股票信息的操作
            # print(user_input)
            stock_name = extract_stock_names(user_input)
            stock_name = stock_name[0]
            print(stock_name)
            stock_code = find_stock_code(stock_name)
            print(stock_code)
            information = fetch_stock_data(stock_code)
            print(information)
            # print("调用股票API")

if wxAPI:
    chatbot = ChatBot("79")
    answer = chatbot.chat(user_input)
    print(answer)
    wxAPI = True
